var api = require("../../../../api.js"),
  app = getApp();
Page({
  data: {
    region: [],
    tagArr: [],
    tag: 0,
    person: '',
    phone: '',
    address: '',
    multiIndex: [0,0,0],
  },
  onLoad:function(e){
    var that = this;
    var uaid = e.id;
    that.setData({
      uaid: e.id
    });
    if (uaid){
      app.request({
        url: api.default.addslist,
        method: "get",
        data: {
          uid: wx.getStorageSync('uid'),
          uaid: uaid,
          bid:wx.getStorageSync('bid')
        },
        success: function (res) {
         
          console.log(res,'addslist');
          var data = res.data[0];
          that.setData({
            person: data.name
          });
          that.setData({
            phone: data.phone
          });
          that.setData({
            region: [
              data.province,
              data.city,
              data.area
            ]
          });
          that.setData({
            address: data.address
          });
        }
      })
    };
    app.request({
      url: api.default.getarea,
      method: "post",
      data: {
        bid: wx.getStorageSync('bid')
      },
      success: function (res) {
        var areaList = res.data;
        that.setData({
          areaList: areaList,
          multiArray: [areaList[0], areaList[1][0], areaList[2][0][0]]
        })
      }
    })
    //是否区域限制
    app.request({
      url: api.default.areaLimit,
      method: "post",
      data: {
        bid: wx.getStorageSync('bid')
      },
      success: function (res) {
        console.log(res,'limitlimit');
        // var areaList = res.data;
        that.setData({
          limit: res.limit,
        })
      }
    })
  },
  //没有区域限制
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  //有区域限制
  bindMultiPickerChange: function(e){
    var that =  this;
    let list = e.detail.value;
    let multiArray = this.data.multiArray;
    that.setData({
      region: [multiArray[0][list[0]], multiArray[1][list[1]], multiArray[2][list[1]]]
    });
  },
  // 选择时
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        for (let i = 0; i <= that.data.areaList[0].length - 1; i++) {
          if (i == e.detail.value) {
            data.multiArray[1] = that.data.areaList[1][i];
            data.multiArray[2] = that.data.areaList[2][i][0];
          };
        };
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
      case 1:
        for (let i = 0; i <= that.data.areaList[1].length - 1; i++) {
          if (i == e.detail.value) {
            // data.multiArray[0] = that.data.areaList[];
            data.multiArray[2] = that.data.areaList[2][this.data.multiIndex[0]][i];
          };
        }
        data.multiIndex[2] = 0;
        break;
    }
    this.setData(data);
  },
  checkTag: function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      tag: index
    });
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "person") {
      that.setData({
        person: inputVlaue
      });
    }
    if (inputName == "identifyingCode") {
      that.setData({
        identifyingCode: inputVlaue
      });
    }
    if (inputName == "phone") {
      that.setData({
        phone: inputVlaue
      });
    }
    if (inputName == "address") {
      that.setData({
        address: inputVlaue
      });
    }
  },
  sendRequest: function () {
    var region = this.data.region;
    var address = this.data.address;
    var phone = this.data.phone;
    var person = this.data.person;
      app.request({
        url: api.default.addsadd,
        method: "POST",
        data: {
          uid: wx.getStorageSync('uid'),
          address: address,
          name: person,
          phone: phone,
          province: region[0],
          city: region[1],
          area: region[2],
          default: 0,
          bid: wx.getStorageSync('bid')
        },
        success: function (res) {
          if (res.data.success==1){
            wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 3000,
                success: function(){
                  //  回退
                  wx.navigateBack({}) 
                }
              })  
          } 
        }
      })
  },
  UpsendRequest: function (e) {
    var uaid = e.currentTarget.dataset.id;
    var region = this.data.region;
    var address = this.data.address;
    var phone = this.data.phone;
    var person = this.data.person;
    app.request({
      url: api.default.siteupdate,
      method: "POST",
      data: {
        uaid: uaid,
        address: address,
        name: person,
        phone: phone,
        province: region[0],
        city: region[1],
        area: region[2]
      },
      success: function (res) {
        if (res.data.success == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 10000,
            success: function () {
              // 回退
              wx.navigateBack({}) 
            }
          })
        }
      }
    })
  },
  validate: function () {
    var that = this;

    if (!that.data.person || that.data.person.length <= 0) {
      wx.showToast({
        title: '收货人不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    var reg = new RegExp('^[1][3,5,8][0-9]{9}$');
    if (!reg.test(that.data.phone)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    if (that.data.region.length <= 0) {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    if (!that.data.address || that.data.address.length <= 0) {
      wx.showToast({
        title: '地址不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    return true;
  }
  ,
  adds:function(){
    var that=this;
  
    wx.chooseAddress({
      success:function(res){
        var region=Array();
        region.push(res.provinceName),
        region.push(res.cityName),
        region.push(res.countyName),
        that.setData({
      
            person:res.userName,
            address:res.detailInfo,
         
            phone:res.telNumber,
            region
        })
      }
    })
  }
})